package com.javabasic.serialize.service;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.javabasic.serialize.deserializer.UserDeserializer;
import com.javabasic.serialize.model.BlogContent;
import com.javabasic.serialize.model.ForumContent;
import com.javabasic.serialize.model.HttpUrl;
import com.javabasic.serialize.model.HttpsUrl;
import com.javabasic.serialize.model.Sex;
import com.javabasic.serialize.model.UrlProtocolType;
import com.javabasic.serialize.model.User;
import com.javabasic.serialize.model.VideoContent;
import com.javabasic.serialize.model.WebSite;
import com.javabasic.serialize.model.WebSiteEnum;
import com.javabasic.serialize.serializer.UserSerializer;
import com.javabasic.serialize.util.SerializeUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2021/11/28 4:35 下午
 */
public class SerializeService {


    /**
     * 普通序列化与反序列化的场景
     */
    @Test
    public void demo1() throws IOException {

        // 序列化
        List<WebSite> webSites = modelProvider();
        String json = SerializeUtils.getObjectMapper().writeValueAsString(webSites);
        System.out.println(json);

        System.out.println("========================");

        // 反序列化
        List<String> webSitesJson = jsonProvider();
        List<WebSite> webSiteList = SerializeUtils.getObjectMapper().readValue(webSitesJson.toString(), new TypeReference<List<WebSite>>() {
        });
        for (WebSite webSite : webSiteList) {
            System.out.println(webSite);
        }

    }


    /**
     * 提供Json数据
     * @return
     */
    public List<String> jsonProvider() {
        List<String> json = new ArrayList<>();

        HttpsUrl https = HttpsUrl.builder()
                .httpsPrefix("https")
                .urlProtocolType(UrlProtocolType.HTTPS)
                .build();
        HttpUrl http = HttpUrl.builder()
                .httpPrefix("http")
                .urlProtocolType(UrlProtocolType.HTTP)
                .build();

        ForumContent forumContent = ForumContent.builder()
                .content("mac 笔记本测试使用")
                .titleName("mac M1 还有这种功能！！！！！")
                .build();
        WebSite zhihu = WebSite.builder()
                .url("https://www.zhihu.com/")
                .name("知乎")
                .webSiteEnum(WebSiteEnum.FORUM)
                .content(forumContent)
                .urlType(https)
                .build();

        VideoContent videoContent = VideoContent.builder()
                .videoName("仙王的日常生活")
                .build();
        WebSite bilibili = WebSite.builder()
                .url("https://www.bilibili.com/")
                .name("B站")
                .webSiteEnum(WebSiteEnum.VIDEO)
                .content(videoContent)
                .urlType(https)
                .build();

        BlogContent blogContent = BlogContent.builder()
                .author("淮上树")
                .flowers(322)
                .build();
        WebSite cnblogs = WebSite.builder()
                .url("https://www.cnblogs.com/")
                .name("博客园")
                .webSiteEnum(WebSiteEnum.BLOG)
                .content(blogContent)
                .urlType(http)
                .build();

        json.add(JSONUtil.parseObj(zhihu,false).toString());
        json.add(JSONUtil.parseObj(bilibili,false).toString());
        json.add(JSONUtil.parseObj(cnblogs,false).toString());

        return json;
    }

    /**
     * 提供模型数据
     * @return
     */
    public List<WebSite> modelProvider() {
        List<WebSite> webSites = new ArrayList<>();
        HttpsUrl https = HttpsUrl.builder()
                .httpsPrefix("https")
                .urlProtocolType(UrlProtocolType.HTTPS)
                .build();
        HttpUrl http = HttpUrl.builder()
                .httpPrefix("http")
                .urlProtocolType(UrlProtocolType.HTTP)
                .build();

        ForumContent forumContent = ForumContent.builder()
                .content("mac 笔记本测试使用")
                .titleName("mac M1 还有这种功能！！！！！")
                .build();
        WebSite zhihu = WebSite.builder()
                .url("https://www.zhihu.com/")
                .name("知乎")
                .webSiteEnum(WebSiteEnum.FORUM)
                .content(forumContent)
                .urlType(https)
                .build();

        VideoContent videoContent = VideoContent.builder()
                .videoName("仙王的日常生活")
                .build();
        WebSite bilibili = WebSite.builder()
                .url("https://www.bilibili.com/")
                .name("B站")
                .webSiteEnum(WebSiteEnum.VIDEO)
                .content(videoContent)
                .urlType(https)
                .build();

        BlogContent blogContent = BlogContent.builder()
                .author("淮上树")
                .flowers(322)
                .build();
        WebSite cnblogs = WebSite.builder()
                .url("https://www.cnblogs.com/")
                .name("博客园")
                .webSiteEnum(WebSiteEnum.BLOG)
                .content(blogContent)
                .urlType(http)
                .build();

        webSites.add(zhihu);
        webSites.add(bilibili);
        webSites.add(cnblogs);

        return webSites;
    }


    @Test
    public void demo2() throws JsonProcessingException {
        User mirXiong = User.builder()
                .age(24)
                .userName("mir_xiong")
                .sex(Sex.MALE)
                .build();
        String mirXiongJson = SerializeUtils.getObjectMapper().writeValueAsString(mirXiong);
        System.out.println(mirXiongJson);
    }

    @Test
    public void demo3() throws IOException {
        ObjectMapper objectMapper = SerializeUtils.getObjectMapper();
//        SimpleModule module = new SimpleModule("UserDeserializer", new Version(3, 1, 8, null, null, null));
        SimpleModule module = new SimpleModule("UserModel");
        module.addDeserializer(User.class,new UserDeserializer());
        module.addSerializer(User.class, new UserSerializer());
        objectMapper.registerModule(module);

        User mirXiong = User.builder()
                .age(24)
                .userName("mir_xiong")
                .sex(Sex.MALE)
                .build();
        String mirXiongJson = objectMapper.writeValueAsString(mirXiong);
        System.out.println(mirXiongJson);

        User user = objectMapper.readValue(mirXiongJson, User.class);
        System.out.println(user);
    }

}
